home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / calendar.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  28KB  |  760 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Calendar printing functions
  5.  
  6. Note when comparing these calendars to the ones printed by cal(1): By
  7. default, these calendars have Monday as the first day of the week, and
  8. Sunday as the last (the European convention). Use setfirstweekday() to
  9. set the first day of the week (0=Monday, 6=Sunday).'''
  10. import sys
  11. import datetime
  12. import locale as _locale
  13. __all__ = [
  14.     'IllegalMonthError',
  15.     'IllegalWeekdayError',
  16.     'setfirstweekday',
  17.     'firstweekday',
  18.     'isleap',
  19.     'leapdays',
  20.     'weekday',
  21.     'monthrange',
  22.     'monthcalendar',
  23.     'prmonth',
  24.     'month',
  25.     'prcal',
  26.     'calendar',
  27.     'timegm',
  28.     'month_name',
  29.     'month_abbr',
  30.     'day_name',
  31.     'day_abbr']
  32. error = ValueError
  33.  
  34. class IllegalMonthError(ValueError):
  35.     
  36.     def __init__(self, month):
  37.         self.month = month
  38.  
  39.     
  40.     def __str__(self):
  41.         return 'bad month number %r; must be 1-12' % self.month
  42.  
  43.  
  44.  
  45. class IllegalWeekdayError(ValueError):
  46.     
  47.     def __init__(self, weekday):
  48.         self.weekday = weekday
  49.  
  50.     
  51.     def __str__(self):
  52.         return 'bad weekday number %r; must be 0 (Monday) to 6 (Sunday)' % self.weekday
  53.  
  54.  
  55. January = 1
  56. February = 2
  57. mdays = [
  58.     0,
  59.     31,
  60.     28,
  61.     31,
  62.     30,
  63.     31,
  64.     30,
  65.     31,
  66.     31,
  67.     30,
  68.     31,
  69.     30,
  70.     31]
  71.  
  72. class _localized_month:
  73.     _months = [ datetime.date(2001, i + 1, 1).strftime for i in range(12) ]
  74.     _months.insert(0, (lambda x: ''))
  75.     
  76.     def __init__(self, format):
  77.         self.format = format
  78.  
  79.     
  80.     def __getitem__(self, i):
  81.         funcs = self._months[i]
  82.         if isinstance(i, slice):
  83.             return [ f(self.format) for f in funcs ]
  84.         return funcs(self.format)
  85.  
  86.     
  87.     def __len__(self):
  88.         return 13
  89.  
  90.  
  91.  
  92. class _localized_day:
  93.     _days = [ datetime.date(2001, 1, i + 1).strftime for i in range(7) ]
  94.     
  95.     def __init__(self, format):
  96.         self.format = format
  97.  
  98.     
  99.     def __getitem__(self, i):
  100.         funcs = self._days[i]
  101.         if isinstance(i, slice):
  102.             return [ f(self.format) for f in funcs ]
  103.         return funcs(self.format)
  104.  
  105.     
  106.     def __len__(self):
  107.         return 7
  108.  
  109.  
  110. day_name = _localized_day('%A')
  111. day_abbr = _localized_day('%a')
  112. month_name = _localized_month('%B')
  113. month_abbr = _localized_month('%b')
  114. (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
  115.  
  116. def isleap(year):
  117.     '''Return 1 for leap years, 0 for non-leap years.'''
  118.     if not year % 4 == 0 and year % 100 != 0:
  119.         pass
  120.     return year % 400 == 0
  121.  
  122.  
  123. def leapdays(y1, y2):
  124.     '''Return number of leap years in range [y1, y2).
  125.        Assume y1 <= y2.'''
  126.     y1 -= 1
  127.     y2 -= 1
  128.     return (y2 // 4 - y1 // 4 - y2 // 100 - y1 // 100) + (y2 // 400 - y1 // 400)
  129.  
  130.  
  131. def weekday(year, month, day):
  132.     '''Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
  133.        day (1-31).'''
  134.     return datetime.date(year, month, day).weekday()
  135.  
  136.  
  137. def monthrange(year, month):
  138.     '''Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
  139.        year, month.'''
  140.     if month <= month:
  141.         pass
  142.     elif not month <= 12:
  143.         raise IllegalMonthError(month)
  144.     
  145.     day1 = weekday(year, month, 1)
  146.     if month == February:
  147.         pass
  148.     ndays = mdays[month] + isleap(year)
  149.     return (day1, ndays)
  150.  
  151.  
  152. class Calendar(object):
  153.     """
  154.     Base calendar class. This class doesn't do any formatting. It simply
  155.     provides data to subclasses.
  156.     """
  157.     
  158.     def __init__(self, firstweekday = 0):
  159.         self.firstweekday = firstweekday
  160.  
  161.     
  162.     def getfirstweekday(self):
  163.         return self._firstweekday % 7
  164.  
  165.     
  166.     def setfirstweekday(self, firstweekday):
  167.         self._firstweekday = firstweekday
  168.  
  169.     firstweekday = property(getfirstweekday, setfirstweekday)
  170.     
  171.     def iterweekdays(self):
  172.         '''
  173.         Return a iterator for one week of weekday numbers starting with the
  174.         configured first one.
  175.         '''
  176.         for i in range(self.firstweekday, self.firstweekday + 7):
  177.             yield i % 7
  178.         
  179.  
  180.     
  181.     def itermonthdates(self, year, month):
  182.         '''
  183.         Return an iterator for one month. The iterator will yield datetime.date
  184.         values and will always iterate through complete weeks, so it will yield
  185.         dates outside the specified month.
  186.         '''
  187.         date = datetime.date(year, month, 1)
  188.         days = (date.weekday() - self.firstweekday) % 7
  189.         date -= datetime.timedelta(days = days)
  190.         oneday = datetime.timedelta(days = 1)
  191.         while True:
  192.             yield date
  193.             date += oneday
  194.             if date.month != month and date.weekday() == self.firstweekday:
  195.                 break
  196.                 continue
  197.  
  198.     
  199.     def itermonthdays2(self, year, month):
  200.         '''
  201.         Like itermonthdates(), but will yield (day number, weekday number)
  202.         tuples. For days outside the specified month the day number is 0.
  203.         '''
  204.         for date in self.itermonthdates(year, month):
  205.             if date.month != month:
  206.                 yield (0, date.weekday())
  207.                 continue
  208.             yield (date.day, date.weekday())
  209.         
  210.  
  211.     
  212.     def itermonthdays(self, year, month):
  213.         '''
  214.         Like itermonthdates(), but will yield day numbers. For days outside
  215.         the specified month the day number is 0.
  216.         '''
  217.         for date in self.itermonthdates(year, month):
  218.             if date.month != month:
  219.                 yield 0
  220.                 continue
  221.             yield date.day
  222.         
  223.  
  224.     
  225.     def monthdatescalendar(self, year, month):
  226.         """
  227.         Return a matrix (list of lists) representing a month's calendar.
  228.         Each row represents a week; week entries are datetime.date values.
  229.         """
  230.         dates = list(self.itermonthdates(year, month))
  231.         return [ dates[i:i + 7] for i in range(0, len(dates), 7) ]
  232.  
  233.     
  234.     def monthdays2calendar(self, year, month):
  235.         """
  236.         Return a matrix representing a month's calendar.
  237.         Each row represents a week; week entries are
  238.         (day number, weekday number) tuples. Day numbers outside this month
  239.         are zero.
  240.         """
  241.         days = list(self.itermonthdays2(year, month))
  242.         return [ days[i:i + 7] for i in range(0, len(days), 7) ]
  243.  
  244.     
  245.     def monthdayscalendar(self, year, month):
  246.         """
  247.         Return a matrix representing a month's calendar.
  248.         Each row represents a week; days outside this month are zero.
  249.         """
  250.         days = list(self.itermonthdays(year, month))
  251.         return [ days[i:i + 7] for i in range(0, len(days), 7) ]
  252.  
  253.     
  254.     def yeardatescalendar(self, year, width = 3):
  255.         '''
  256.         Return the data for the specified year ready for formatting. The return
  257.         value is a list of month rows. Each month row contains upto width months.
  258.         Each month contains between 4 and 6 weeks and each week contains 1-7
  259.         days. Days are datetime.date objects.
  260.         '''
  261.         months = [ self.monthdatescalendar(year, i) for i in range(January, January + 12) ]
  262.         return [ months[i:i + width] for i in range(0, len(months), width) ]
  263.  
  264.     
  265.     def yeardays2calendar(self, year, width = 3):
  266.         '''
  267.         Return the data for the specified year ready for formatting (similar to
  268.         yeardatescalendar()). Entries in the week lists are
  269.         (day number, weekday number) tuples. Day numbers outside this month are
  270.         zero.
  271.         '''
  272.         months = [ self.monthdays2calendar(year, i) for i in range(January, January + 12) ]
  273.         return [ months[i:i + width] for i in range(0, len(months), width) ]
  274.  
  275.     
  276.     def yeardayscalendar(self, year, width = 3):
  277.         '''
  278.         Return the data for the specified year ready for formatting (similar to
  279.         yeardatescalendar()). Entries in the week lists are day numbers.
  280.         Day numbers outside this month are zero.
  281.         '''
  282.         months = [ self.monthdayscalendar(year, i) for i in range(January, January + 12) ]
  283.         return [ months[i:i + width] for i in range(0, len(months), width) ]
  284.  
  285.  
  286.  
  287. class TextCalendar(Calendar):
  288.     '''
  289.     Subclass of Calendar that outputs a calendar as a simple plain text
  290.     similar to the UNIX program cal.
  291.     '''
  292.     
  293.     def prweek(self, theweek, width):
  294.         '''
  295.         Print a single week (no newline).
  296.         '''
  297.         print self.formatweek(theweek, width),
  298.  
  299.     
  300.     def formatday(self, day, weekday, width):
  301.         '''
  302.         Returns a formatted day.
  303.         '''
  304.         if day == 0:
  305.             s = ''
  306.         else:
  307.             s = '%2i' % day
  308.         return s.center(width)
  309.  
  310.     
  311.     def formatweek(self, theweek, width):
  312.         '''
  313.         Returns a single week in a string (no newline).
  314.         '''
  315.         return (None, ' '.join)((lambda .0: for d, wd in .0:
  316. self.formatday(d, wd, width))(theweek))
  317.  
  318.     
  319.     def formatweekday(self, day, width):
  320.         '''
  321.         Returns a formatted week day name.
  322.         '''
  323.         if width >= 9:
  324.             names = day_name
  325.         else:
  326.             names = day_abbr
  327.         return names[day][:width].center(width)
  328.  
  329.     
  330.     def formatweekheader(self, width):
  331.         '''
  332.         Return a header for a week.
  333.         '''
  334.         return (None, ' '.join)((lambda .0: for i in .0:
  335. self.formatweekday(i, width))(self.iterweekdays()))
  336.  
  337.     
  338.     def formatmonthname(self, theyear, themonth, width, withyear = True):
  339.         '''
  340.         Return a formatted month name.
  341.         '''
  342.         s = month_name[themonth]
  343.         if withyear:
  344.             s = '%s %r' % (s, theyear)
  345.         
  346.         return s.center(width)
  347.  
  348.     
  349.     def prmonth(self, theyear, themonth, w = 0, l = 0):
  350.         """
  351.         Print a month's calendar.
  352.         """
  353.         print self.formatmonth(theyear, themonth, w, l),
  354.  
  355.     
  356.     def formatmonth(self, theyear, themonth, w = 0, l = 0):
  357.         """
  358.         Return a month's calendar string (multi-line).
  359.         """
  360.         w = max(2, w)
  361.         l = max(1, l)
  362.         s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
  363.         s = s.rstrip()
  364.         s += '\n' * l
  365.         s += self.formatweekheader(w).rstrip()
  366.         s += '\n' * l
  367.         for week in self.monthdays2calendar(theyear, themonth):
  368.             s += self.formatweek(week, w).rstrip()
  369.             s += '\n' * l
  370.         
  371.         return s
  372.  
  373.     
  374.     def formatyear(self, theyear, w = 2, l = 1, c = 6, m = 3):
  375.         """
  376.         Returns a year's calendar as a multi-line string.
  377.         """
  378.         w = max(2, w)
  379.         l = max(1, l)
  380.         c = max(2, c)
  381.         colwidth = (w + 1) * 7 - 1
  382.         v = []
  383.         a = v.append
  384.         a(repr(theyear).center(colwidth * m + c * (m - 1)).rstrip())
  385.         a('\n' * l)
  386.         header = self.formatweekheader(w)
  387.         for i, row in enumerate(self.yeardays2calendar(theyear, m)):
  388.             months = range(m * i + 1, min(m * (i + 1) + 1, 13))
  389.             a('\n' * l)
  390.             names = (lambda .0: for k in .0:
  391. self.formatmonthname(theyear, k, colwidth, False))(months)
  392.             a(formatstring(names, colwidth, c).rstrip())
  393.             a('\n' * l)
  394.             headers = (lambda .0: for k in .0:
  395. header)(months)
  396.             a(formatstring(headers, colwidth, c).rstrip())
  397.             a('\n' * l)
  398.             height = max((lambda .0: for cal in .0:
  399. len(cal))(row))
  400.             for j in range(height):
  401.                 weeks = []
  402.                 for cal in row:
  403.                     if j >= len(cal):
  404.                         weeks.append('')
  405.                         continue
  406.                     ((None, None, None),)
  407.                     weeks.append(self.formatweek(cal[j], w))
  408.                 
  409.                 a(formatstring(weeks, colwidth, c).rstrip())
  410.                 a('\n' * l)
  411.             
  412.         
  413.         return ''.join(v)
  414.  
  415.     
  416.     def pryear(self, theyear, w = 0, l = 0, c = 6, m = 3):
  417.         """Print a year's calendar."""
  418.         print self.formatyear(theyear, w, l, c, m)
  419.  
  420.  
  421.  
  422. class HTMLCalendar(Calendar):
  423.     '''
  424.     This calendar returns complete HTML pages.
  425.     '''
  426.     cssclasses = [
  427.         'mon',
  428.         'tue',
  429.         'wed',
  430.         'thu',
  431.         'fri',
  432.         'sat',
  433.         'sun']
  434.     
  435.     def formatday(self, day, weekday):
  436.         '''
  437.         Return a day as a table cell.
  438.         '''
  439.         if day == 0:
  440.             return '<td class="noday"> </td>'
  441.         return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)
  442.  
  443.     
  444.     def formatweek(self, theweek):
  445.         '''
  446.         Return a complete week as a table row.
  447.         '''
  448.         s = (''.join,)((lambda .0: for d, wd in .0:
  449. self.formatday(d, wd))(theweek))
  450.         return '<tr>%s</tr>' % s
  451.  
  452.     
  453.     def formatweekday(self, day):
  454.         '''
  455.         Return a weekday name as a table header.
  456.         '''
  457.         return '<th class="%s">%s</th>' % (self.cssclasses[day], day_abbr[day])
  458.  
  459.     
  460.     def formatweekheader(self):
  461.         '''
  462.         Return a header for a week as a table row.
  463.         '''
  464.         s = (''.join,)((lambda .0: for i in .0:
  465. self.formatweekday(i))(self.iterweekdays()))
  466.         return '<tr>%s</tr>' % s
  467.  
  468.     
  469.     def formatmonthname(self, theyear, themonth, withyear = True):
  470.         '''
  471.         Return a month name as a table row.
  472.         '''
  473.         if withyear:
  474.             s = '%s %s' % (month_name[themonth], theyear)
  475.         else:
  476.             s = '%s' % month_name[themonth]
  477.         return '<tr><th colspan="7" class="month">%s</th></tr>' % s
  478.  
  479.     
  480.     def formatmonth(self, theyear, themonth, withyear = True):
  481.         '''
  482.         Return a formatted month as a table.
  483.         '''
  484.         v = []
  485.         a = v.append
  486.         a('<table border="0" cellpadding="0" cellspacing="0" class="month">')
  487.         a('\n')
  488.         a(self.formatmonthname(theyear, themonth, withyear = withyear))
  489.         a('\n')
  490.         a(self.formatweekheader())
  491.         a('\n')
  492.         for week in self.monthdays2calendar(theyear, themonth):
  493.             a(self.formatweek(week))
  494.             a('\n')
  495.         
  496.         a('</table>')
  497.         a('\n')
  498.         return ''.join(v)
  499.  
  500.     
  501.     def formatyear(self, theyear, width = 3):
  502.         '''
  503.         Return a formatted year as a table of tables.
  504.         '''
  505.         v = []
  506.         a = v.append
  507.         width = max(width, 1)
  508.         a('<table border="0" cellpadding="0" cellspacing="0" class="year">')
  509.         a('\n')
  510.         a('<tr><th colspan="%d" class="year">%s</th></tr>' % (width, theyear))
  511.         for i in range(January, January + 12, width):
  512.             months = range(i, min(i + width, 13))
  513.             a('<tr>')
  514.             for m in months:
  515.                 a('<td>')
  516.                 a(self.formatmonth(theyear, m, withyear = False))
  517.                 a('</td>')
  518.             
  519.             a('</tr>')
  520.         
  521.         a('</table>')
  522.         return ''.join(v)
  523.  
  524.     
  525.     def formatyearpage(self, theyear, width = 3, css = 'calendar.css', encoding = None):
  526.         '''
  527.         Return a formatted year as a complete HTML page.
  528.         '''
  529.         if encoding is None:
  530.             encoding = sys.getdefaultencoding()
  531.         
  532.         v = []
  533.         a = v.append
  534.         a('<?xml version="1.0" encoding="%s"?>\n' % encoding)
  535.         a('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n')
  536.         a('<html>\n')
  537.         a('<head>\n')
  538.         a('<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n' % encoding)
  539.         if css is not None:
  540.             a('<link rel="stylesheet" type="text/css" href="%s" />\n' % css)
  541.         
  542.         a('<title>Calendar for %d</title>\n' % theyear)
  543.         a('</head>\n')
  544.         a('<body>\n')
  545.         a(self.formatyear(theyear, width))
  546.         a('</body>\n')
  547.         a('</html>\n')
  548.         return ''.join(v).encode(encoding, 'xmlcharrefreplace')
  549.  
  550.  
  551.  
  552. class TimeEncoding:
  553.     
  554.     def __init__(self, locale):
  555.         self.locale = locale
  556.  
  557.     
  558.     def __enter__(self):
  559.         self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale)
  560.         return _locale.getlocale(_locale.LC_TIME)[1]
  561.  
  562.     
  563.     def __exit__(self, *args):
  564.         _locale.setlocale(_locale.LC_TIME, self.oldlocale)
  565.  
  566.  
  567.  
  568. class LocaleTextCalendar(TextCalendar):
  569.     '''
  570.     This class can be passed a locale name in the constructor and will return
  571.     month and weekday names in the specified locale. If this locale includes
  572.     an encoding all strings containing month and weekday names will be returned
  573.     as unicode.
  574.     '''
  575.     
  576.     def __init__(self, firstweekday = 0, locale = None):
  577.         TextCalendar.__init__(self, firstweekday)
  578.         if locale is None:
  579.             locale = _locale.getdefaultlocale()
  580.         
  581.         self.locale = locale
  582.  
  583.     
  584.     def formatweekday(self, day, width):
  585.         
  586.         try:
  587.             encoding = _[1]
  588.             name = names[day]
  589.             if encoding is not None:
  590.                 name = name.decode(encoding)
  591.             
  592.             return name[:width].center(width)
  593.         finally:
  594.             pass
  595.  
  596.  
  597.     
  598.     def formatmonthname(self, theyear, themonth, width, withyear = True):
  599.         
  600.         try:
  601.             encoding = _[1]
  602.             s = month_name[themonth]
  603.             if withyear:
  604.                 s = '%s %r' % (s, theyear)
  605.             
  606.             return s.center(width)
  607.         finally:
  608.             pass
  609.  
  610.  
  611.  
  612.  
  613. class LocaleHTMLCalendar(HTMLCalendar):
  614.     '''
  615.     This class can be passed a locale name in the constructor and will return
  616.     month and weekday names in the specified locale. If this locale includes
  617.     an encoding all strings containing month and weekday names will be returned
  618.     as unicode.
  619.     '''
  620.     
  621.     def __init__(self, firstweekday = 0, locale = None):
  622.         HTMLCalendar.__init__(self, firstweekday)
  623.         if locale is None:
  624.             locale = _locale.getdefaultlocale()
  625.         
  626.         self.locale = locale
  627.  
  628.     
  629.     def formatweekday(self, day):
  630.         
  631.         try:
  632.             encoding = _[1]
  633.             s = day_abbr[day]
  634.             return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
  635.         finally:
  636.             pass
  637.  
  638.  
  639.     
  640.     def formatmonthname(self, theyear, themonth, withyear = True):
  641.         
  642.         try:
  643.             encoding = _[1]
  644.             s = month_name[themonth]
  645.             if withyear:
  646.                 s = '%s %s' % (s, theyear)
  647.             
  648.             return '<tr><th colspan="7" class="month">%s</th></tr>' % s
  649.         finally:
  650.             pass
  651.  
  652.  
  653.  
  654. c = TextCalendar()
  655. firstweekday = c.getfirstweekday
  656.  
  657. def setfirstweekday(firstweekday):
  658.     if firstweekday <= firstweekday:
  659.         pass
  660.     elif not firstweekday <= SUNDAY:
  661.         raise IllegalWeekdayError(firstweekday)
  662.     
  663.     c.firstweekday = firstweekday
  664.  
  665. monthcalendar = c.monthdayscalendar
  666. prweek = c.prweek
  667. week = c.formatweek
  668. weekheader = c.formatweekheader
  669. prmonth = c.prmonth
  670. month = c.formatmonth
  671. calendar = c.formatyear
  672. prcal = c.pryear
  673. _colwidth = 20
  674. _spacing = 6
  675.  
  676. def format(cols, colwidth = _colwidth, spacing = _spacing):
  677.     '''Prints multi-column formatting for year calendars'''
  678.     print formatstring(cols, colwidth, spacing)
  679.  
  680.  
  681. def formatstring(cols, colwidth = _colwidth, spacing = _spacing):
  682.     '''Returns a string formatted from n strings, centered within n columns.'''
  683.     spacing *= ' '
  684.     return (spacing.join,)((lambda .0: for c in .0:
  685. c.center(colwidth))(cols))
  686.  
  687. EPOCH = 1970
  688. _EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
  689.  
  690. def timegm(tuple):
  691.     '''Unrelated but handy function to calculate Unix timestamp from GMT.'''
  692.     (year, month, day, hour, minute, second) = tuple[:6]
  693.     days = (datetime.date(year, month, 1).toordinal() - _EPOCH_ORD) + day - 1
  694.     hours = days * 24 + hour
  695.     minutes = hours * 60 + minute
  696.     seconds = minutes * 60 + second
  697.     return seconds
  698.  
  699.  
  700. def main(args):
  701.     import optparse as optparse
  702.     parser = optparse.OptionParser(usage = 'usage: %prog [options] [year [month]]')
  703.     parser.add_option('-w', '--width', dest = 'width', type = 'int', default = 2, help = 'width of date column (default 2, text only)')
  704.     parser.add_option('-l', '--lines', dest = 'lines', type = 'int', default = 1, help = 'number of lines for each week (default 1, text only)')
  705.     parser.add_option('-s', '--spacing', dest = 'spacing', type = 'int', default = 6, help = 'spacing between months (default 6, text only)')
  706.     parser.add_option('-m', '--months', dest = 'months', type = 'int', default = 3, help = 'months per row (default 3, text only)')
  707.     parser.add_option('-c', '--css', dest = 'css', default = 'calendar.css', help = 'CSS to use for page (html only)')
  708.     parser.add_option('-L', '--locale', dest = 'locale', default = None, help = 'locale to be used from month and weekday names')
  709.     parser.add_option('-e', '--encoding', dest = 'encoding', default = None, help = 'Encoding to use for output')
  710.     parser.add_option('-t', '--type', dest = 'type', default = 'text', choices = ('text', 'html'), help = 'output type (text or html)')
  711.     (options, args) = parser.parse_args(args)
  712.     if options.locale and not (options.encoding):
  713.         parser.error('if --locale is specified --encoding is required')
  714.         sys.exit(1)
  715.     
  716.     locale = (options.locale, options.encoding)
  717.     if options.type == 'html':
  718.         if options.locale:
  719.             cal = LocaleHTMLCalendar(locale = locale)
  720.         else:
  721.             cal = HTMLCalendar()
  722.         encoding = options.encoding
  723.         if encoding is None:
  724.             encoding = sys.getdefaultencoding()
  725.         
  726.         optdict = dict(encoding = encoding, css = options.css)
  727.         if len(args) == 1:
  728.             print cal.formatyearpage(datetime.date.today().year, **optdict)
  729.         elif len(args) == 2:
  730.             print cal.formatyearpage(int(args[1]), **optdict)
  731.         else:
  732.             parser.error('incorrect number of arguments')
  733.             sys.exit(1)
  734.     elif options.locale:
  735.         cal = LocaleTextCalendar(locale = locale)
  736.     else:
  737.         cal = TextCalendar()
  738.     optdict = dict(w = options.width, l = options.lines)
  739.     if len(args) != 3:
  740.         optdict['c'] = options.spacing
  741.         optdict['m'] = options.months
  742.     
  743.     if len(args) == 1:
  744.         result = cal.formatyear(datetime.date.today().year, **optdict)
  745.     elif len(args) == 2:
  746.         result = cal.formatyear(int(args[1]), **optdict)
  747.     elif len(args) == 3:
  748.         result = cal.formatmonth(int(args[1]), int(args[2]), **optdict)
  749.     else:
  750.         parser.error('incorrect number of arguments')
  751.         sys.exit(1)
  752.     if options.encoding:
  753.         result = result.encode(options.encoding)
  754.     
  755.     print result
  756.  
  757. if __name__ == '__main__':
  758.     main(sys.argv)
  759.  
  760.